/**
 * copyright(c) 2013 www.jiongshibaoliao.com, All Rights Reserved.
 * project: jiongshibaoliao 
 * author: 曾帅
 * date: 2014-1-25 上午10:35:07
 * version: v1.0
 */

package com.jiongshibaoliao.util;

import java.util.Date;
import java.util.Properties;

import javax.mail.Address;
import javax.mail.BodyPart;
import javax.mail.Message;
import javax.mail.MessagingException;
import javax.mail.Multipart;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeBodyPart;
import javax.mail.internet.MimeMessage;
import javax.mail.internet.MimeMultipart;

import org.apache.log4j.Logger;

import com.jiongshibaoliao.model.bean.MailBean;

/**
 * @project jiongshibaoliao
 * @author 曾帅
 * @version 1.0
 * @date 2014-1-25 上午10:35:07
 */
public class MailUtil {
	
	private static final Logger logger = Logger.getLogger(MailUtil.class);

	/**
	 * 以文本格式发送邮件
	 * @author 曾帅
	 * @version 1.0
	 * @date 2014-1-25 上午10:51:23
	 * @param mailBean
	 * @return boolean
	 */
	public static boolean sendTextMail(MailBean mailBean) {
		MailAuthenticator authenticator = null;
		Properties properties = mailBean.getProperties();
		if (mailBean.isValidate()) {
			authenticator = new MailAuthenticator(mailBean.getUserName(), mailBean.getPassword());
		}
		Session session = Session.getDefaultInstance(properties, authenticator);
		
		try {
			Message message = new MimeMessage(session);
			Address from = new InternetAddress(mailBean.getFromAddress());
			message.setFrom(from);
			Address to = new InternetAddress(mailBean.getToAddress());
			message.setRecipient(Message.RecipientType.TO, to);
			message.setSubject(mailBean.getSubject());
			message.setSentDate(new Date());
			message.setText(mailBean.getContent());
			Transport.send(message);
		} catch (MessagingException e) {
			logger.error("邮件发送失败 - 文本格式：", e);
			return false;
		}
		
		return true;
	}

	/**
	 * 以HTML格式发送邮件
	 * @author 曾帅
	 * @version 1.0
	 * @date 2014-1-25 上午10:51:30
	 * @param mailBean
	 * @return boolean
	 */
	public static boolean sendHtmlMail(MailBean mailBean) {
		MailAuthenticator authenticator = null;
		Properties properties = mailBean.getProperties();
		if (mailBean.isValidate()) {
			authenticator = new MailAuthenticator(mailBean.getUserName(), mailBean.getPassword());
		}
		Session session = Session.getDefaultInstance(properties, authenticator);
		
		try {
			Message message = new MimeMessage(session);
			Address from = new InternetAddress(mailBean.getFromAddress());
			message.setFrom(from);
			Address to = new InternetAddress(mailBean.getToAddress());
			message.setRecipient(Message.RecipientType.TO, to);
			message.setSubject(mailBean.getSubject());
			message.setSentDate(new Date());
			Multipart mainPart = new MimeMultipart();
			BodyPart html = new MimeBodyPart();
			html.setContent(mailBean.getContent(), "text/html; charset=utf-8");
			mainPart.addBodyPart(html);
			message.setContent(mainPart);
			Transport.send(message);
		} catch (MessagingException e) {
			logger.error("邮件发送失败 - HTML格式：", e);
			return false;
		}
		
		return true;
	}
}
